<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 * @property Post_model $post_model
 */
class Landing extends MX_Controller
{
    private $_langCode;

    function __construct()
    {
        parent::__construct();
        $this->load->library('Layout');
        $this->_langCode = $this->mycommon->getLangCode();
    }

    public function index()
    {
        $data = array();
        $this->load->model('option/option_model');
        $this->load->model('category/category_model');
        $this->load->model('post/post_model');
        $this->load->language('landing/landing', $this->_langCode);
        $data['lang'] = $this->lang->line('landing_lang');
        $langCode = $this->mycommon->fetchLangCode($this->_langCode);
        $lstBoxHome = $this->option_model->getOptionByName('lstBoxHome');
        $arrBoxHome = explode(',', $lstBoxHome['value']);
        foreach ($arrBoxHome as $k=>$v) {
            if($tmpCatInfo = $this->category_model->getCategoryInfo($v, $langCode)) {
                $catInfo = $tmpCatInfo[0];
                $path = $this->mycommon->convertToAlias($catInfo['name']) . '_' . $catInfo['id'];
                if($catInfo['href'] == '')
                    $url = site_url($path);
                else
                    $url = $catInfo['href'];
                $catInfo['href'] = $url;
                $data['boxMenu'][$k]['info'] = $catInfo;
                $child = $this->category_model->getChildrenCategory($catInfo['id'], $langCode);
                if($child) {
                    foreach ($child as $_k=>$_v) {
                        $path = $this->mycommon->convertToAlias($_v['name']) . '_' . $_v['id'];
                        if($_v['href'] == '')
                            $url = site_url((($this->_langCode == 'vietnam') ? 'chuyen-muc/' : 'category/') . $path);
                        else
                            $url = $_v['href'];
                        $child[$_k]['href'] = $url;
                    }
                }
                $data['boxMenu'][$k]['child'] = $child;
                $tmpPost = $this->post_model->getPostByCatId($catInfo['id'],5,0, $langCode);
                foreach ($tmpPost as $kP=>$vP) {
                    $path = $this->mycommon->convertToAlias($vP['title']) . '_' . $vP['id'];
                    $tmpPost[$kP]['url'] = site_url($path);
                }
                $data['boxMenu'][$k]['post'] = $tmpPost;
            }
        }
        $data['goodPost'] = $this->post_model->getGoodPost($langCode);
        $data['langCode'] = $this->_langCode;
        $this->layout->view('landing/landing_view', $data);
    }
    
    public function change()
    {
        $lang = $this->input->get('lang');
        $backurl = $this->input->get('back_url');
        $this->load->library('Mycommon');
        if($lang) {
            $this->mycommon->setLangCode($lang);
            if($backurl) {
                $back = urldecode($backurl);
                redirect($back);
            } else {
                redirect('trang-chu');
            }
        }
    }

}
?>
